<template>
  <div>
    <el-row>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="出生时间">
          <el-col :span="7">
            <el-date-picker type="date" placeholder="选择日期"
                            v-model="searchForm.startTime" style="width: 100%;"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="7">
            <el-date-picker type="date" placeholder="选择日期"
                            v-model="searchForm.endTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="searchForm.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="addUser">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" size="mini" @click="delBatch">批量删除</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!--用户数据展示表格 -->
    <el-table :data="tableData" border style="width: 100%"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" label="序号">
      </el-table-column>
      <el-table-column fixed prop="createTime" label="出生日期" width="150"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120" ></el-table-column>
      <el-table-column prop="phone" label="联系方式" width="140"></el-table-column>
      <el-table-column prop="address" label="地址" width="290"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="270"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="delUser(scope.row.id)" type="danger" size="mini">删除</el-button>
          <el-button @click="updateUser(scope.row)" type="warning" size="mini">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页功能 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 表单模态框 -->
    <el-dialog title="用户信息" :visible.sync="userVisible">
      <el-form ref="refUserForm" :model="userForm" label-width="80px">
        <el-form-item label="出生日期" prop="createTime">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="userForm.createTime"
                            style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="userForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userForm.address"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item label="id" prop="id" v-show='hide'>
          <el-input v-model="userForm.id"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "User",
  methods: {
    handleClick(row) {
      console.log(row);
    },
    //条件查询
    search() {
      console.log(this.searchForm)
      this.$http.get("/user/list", this.searchForm).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
      })
    },
    //更改每页显示条数
    sizeChange(val) {
      this.pageSize = val
      this.getUserList(this.currentPage, val)
    },
    //跳转到指定页面
    currentChange(val) {
      this.currentPage = val
      this.getUserList(this.currentPage, this.pageSize)
    },
    //获取所有用户信息
    getUserList(currentPage, pageSize) {
      //获取当前页和每页条数
      this.parameter = {
        "page": currentPage,
        "size": pageSize
      }
      //发送请求
      this.$http.get("/user/list", this.parameter).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
      })
    },
    //绑定新增按钮
    addUser() {
      this.userVisible = true
      if (this.$refs.refUserForm !== undefined) {
        this.$refs.refUserForm.resetFields();
      }
    },
    //绑定删除按钮
    delUser(id) {
      //确认弹框
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
        //继续删除操作
      }).then(() => this.$http.get("/user/delete", {data:{"id":id}}).then(res => {
        //删除成功
        if (res.data.success) {
          this.getUserList(this.currentPage, this.pageSize)
          this.$message({
            message: res.data.msg,
            type: 'warning'
          });
        }
      }))
    },
    //绑定修改按钮
    updateUser(row) {
      this.userVisible = true
      this.$nextTick(() => {
        //复制数据
        let userMsg = JSON.parse(JSON.stringify(row));
        //赋值
        this.userForm = userMsg
      })
    },
    //绑定模态框中的取消按钮
    cancel() {
      this.userVisible = false
    },
    //绑定模态框中的确定按钮
    save() {
      this.userVisible = false
      if (this.userForm.id == null) {
        //执行新增方法
        this.$http.post("/user/add", this.userForm).then(res => {
          //添加成功
          if (res.data.success) {
            this.getUserList(this.currentPage, this.pageSize)
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      } else {
        //执行修改方法
        this.$http.post("/user/update", this.userForm).then(res => {
          //添加成功
          if (res.data.success) {
            this.getUserList(this.currentPage, this.pageSize)
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }
    },
    handleSelectionChange(multipleSelection){
        this.multipleSelection = multipleSelection;
    },
    //绑定批量删除按钮
    delBatch(){
      let arr = []
      console.log(this.multipleSelection)
      this.multipleSelection.map((item) =>  arr.push(item.id))
      console.log(arr)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
        //继续删除操作
      }).then(() => this.$http.get("/user/delete",arr).then(res => {
        //删除成功
        if (res.data.success) {
          this.getUserList(this.currentPage, this.pageSize)
          this.$message({
            message: res.data.msg,
            type: 'warning'
          });
        }
      }))
    }
  },

  data() {
    return {
      //返回用户信息到表格中
      tableData: [],
      //当前页
      currentPage: 1,
      //页面展示的数据条数
      pageSize: 10,
      //提交参数的对象
      parameter: null,
      //总条数
      total: 0,
      pageSizes: [5, 10, 20, 50],
      //表单状态,默认隐藏
      userVisible: false,
      //用户表单信息
      userForm: {
        name: null,
        createTime: null,
        phone: null,
        address: null,
        email: null,
        id: null
      },
      hide: false,
      //搜索框属性
      searchForm: {
        startTime: null,
        endTime: null,
        name: null,
        address: null
      },
      //批量删除的数据集合
      multipleSelection:[]
    }
  },
  mounted() {
    this.getUserList(this.currentPage, this.pageSize)
  }
}
</script>

<style scoped>
</style>
